﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EZMemoLib;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;

namespace Remember.WPF.Views
{
    /// <summary>
    /// Interaction logic for BookEdit.xaml
    /// </summary>
    public partial class BookEdit : System.Windows.Controls.UserControl
    {
        public event EventHandler OnClose;

        public MemoCategory _Category = null;
        public MemoCategory Category
        {
            set {
                _Category = value ;
                BindBook();
            }

        }

        public void LoadBook(int BookID)
        {
            if (BookID > 0)
            {
                Category = bookBus.GetCategory(BookID);
            }
            else
            {
                Category = new MemoCategory { CategoryLevel = (long)CategoryLevel.Book };
            }
        }

        //int categoryLevel = 1; // 0 - series like nce, ce , 1- book like nce1, 2- lesson

        BookBusiness bookBus = BookBusiness.Instance();

        public BookEdit()
        {
            
            InitializeComponent();

            if (_Category != null && _Category.CategoryID > 0)
            { 
                BindBook( );
            }
        }

        void BindBook( )
        {
            this.uiName.Text = _Category.CategoryName ;
            this.uiDesc.Text = _Category.CategoryDesc;
            this.uiSound.Text = _Category.CategorySound ;


            var list =  bookBus.GetLessonList (_Category.CategoryID);

            foreach (var c in list)
                this.listLessons.Items.Add(c);

            this.listLessons.DisplayMemberPath = "CategoryDesc";
            this.listLessons.SelectedValuePath = "CategoryID";
        }


        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (_Category != null)
            {
                Pickup();

                bookBus.SaveCategory (_Category);
            }

        }

        private void Pickup()
        {
            if (_Category == null)
            {
                _Category = new MemoCategory { CategoryLevel = (long)CategoryLevel.Book };
            }

            _Category.CategoryName = this.uiName.Text ;
            _Category.CategorySound = this.uiSound.Text ;
            _Category.CategoryDesc = this.uiDesc.Text ;
            
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            this.uiName.Text = "";
            this.uiSound.Text = "";
            this.uiDesc.Text = "";

        }

        private void listLessons_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cate = listLessons.SelectedItem as MemoCategory;
            if (cate != null)
            {
                uiLessonName.Text = cate.CategoryName ;
                uiLessonDesc.Text = cate.CategoryDesc ; 

            }

        }

        private void btnLessonCreate_Click(object sender, RoutedEventArgs e)
        {
            uiLessonName.Text = "";
            uiLessonDesc.Text = ""; 

            
        }

        private void btnLessonSave_Click(object sender, RoutedEventArgs e)
        {
            if (_Category == null)
            {
                System.Windows.MessageBox.Show("Parent is null!");
            }
            else
            {
                var newCate = new MemoCategory { CategoryLevel = 2,  CategoryName = uiLessonName.Text, CategoryDesc = uiLessonDesc.Text, ParentID = _Category.CategoryID };

                listLessons.Items.Add(newCate);
                bookBus.SaveCategory(newCate);
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            if( OnClose != null)
            {
                OnClose ( sender , e );
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnImportFromLrc_Click(object sender, RoutedEventArgs e)
        {
            long bookID = this._Category.CategoryID;
            string sourceFolder = uiImportSource.Text;

            if (!string.IsNullOrEmpty(sourceFolder))
            {
                int mp3Count = bookBus.ImportMp3Files(bookID, sourceFolder);
                int lrcCount = bookBus.ImportFromLrc(bookID, sourceFolder);
            }
            else
            {
                System.Windows.MessageBox.Show( "Please select a folder include some lrc files"); 
            }
        }

        private void btnImportSourceBrowser_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            var rc = fbd.ShowDialog();

            string folderName = fbd.SelectedPath; //获得选择的文件夹路径
            uiImportSource.Text = fbd.SelectedPath;

        }

        private void uiImportSource_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void uiLessonName_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void btnImportFromBookFiles_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnImportFromBookFilesBrowser_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
